package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.UmsResource;
import com.itheima.mapper.UmsResourceMapper;
import com.itheima.service.UmsResourceService;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 后台资源表 服务实现类
 * </p>
 *
 * @author zhuguo
 * @since 2022-11-10
 */
@DubboService
public class UmsResourceServiceImpl extends ServiceImpl<UmsResourceMapper, UmsResource> implements UmsResourceService {

    @Autowired
    private UmsResourceMapper umsResourceMapper;


    @Override
    public Page<UmsResource> listByPage(Integer pageNum, Integer pageSize, Integer categoryId, String nameKeyword, String urlKeyword) {
        Page<UmsResource> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsResource> lqw = new LambdaQueryWrapper<>();
        lqw.eq(categoryId != null, UmsResource::getCategoryId, categoryId)
                .like(StringUtils.isNotEmpty(nameKeyword), UmsResource::getName, nameKeyword)
                .like(StringUtils.isNotEmpty(urlKeyword), UmsResource::getUrl, urlKeyword);
        return umsResourceMapper.selectPage(page, lqw);
    }

    @Override
    public void createResource(UmsResource umsResource) {
        umsResourceMapper.insert(umsResource);
    }

    @Override
    public List<UmsResource> getAll() {
       return umsResourceMapper.getAll();
    }

    @Override
    public void deleteResourceById(Integer id) {
      umsResourceMapper.deleteById(id);
    }

    @Override
    public void updateResourceById(Integer id, UmsResource umsResource) {
        LambdaQueryWrapper<UmsResource> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsResource::getId,id);
        umsResourceMapper.update(umsResource,lqw);
    }

    @Override
    public UmsResource queryResourceById(Integer id) {
        LambdaQueryWrapper<UmsResource> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsResource::getId,id);
        return umsResourceMapper.selectOne(lqw);
    }

    @Override
    public List<UmsResource> getByIds(List<Long> ids) {
        return umsResourceMapper.selectBatchIds(ids);
    }


}
